package com.own.business.resource.module.qiniu;

import com.own.business.resource.core.base.AbstractObjectStoreUtil;
import com.own.business.resource.core.model.SignatureModel;
import com.own.business.resource.core.model.base.BaseUploadModel;
import com.own.component.common.exception.BusinessSimpleException;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * QiNiuKodoUtil
 *
 * @author chenxueli
 * @date 2023-07-12 09:11:36
 */
@Slf4j
public class QiNiuKodoUtil extends AbstractObjectStoreUtil {

    /**
     * 上传单个文件
     *
     * @param file 文件信息
     * @return 文件存储对象信息
     */
    @Override
    public BaseUploadModel uploadFile(BaseUploadModel file) {
        var auth = getQiniuAuth();
        upload(auth, file);
        return file;
    }

    /**
     * 上传单个文件
     *
     * @param fileList 文件信息列表
     * @return 文件存储对象信息列表
     */
    @Override
    public List<BaseUploadModel> uploadFile(List<BaseUploadModel> fileList) {
        var auth = getQiniuAuth();
        fileList.parallelStream().forEach(file -> upload(auth, file));
        return fileList;
    }

    /**
     * 验证配置是否正常
     *
     * @return 是否正常
     */
    @Override
    public boolean check() {
        var properties = super.getProperties();
        var name = super.getName();
        boolean flag = true;
        if (StringUtils.isEmpty(properties.getKey())) {
            log.error("资源管理{}:七牛云kodo key填写为空", name);
            flag = false;
        }
        if (StringUtils.isEmpty(properties.getSecret())) {
            log.error("资源管理{}:七牛云kodo secret填写为空", name);
            flag = false;
        }
        if (StringUtils.isEmpty(properties.getBucket())) {
            log.error("资源管理{}:七牛云kodo bucket填写为空", name);
            flag = false;
        }
        if (StringUtils.isEmpty(properties.getPosition())) {
            log.error("资源管理{}:七牛云kodo position填写为空", name);
            flag = false;
        }
        return flag;
    }

    /**
     * 获取上传文件签名
     *
     * @param uri 资源路径
     * @return 签名对象
     */
    @Override
    public SignatureModel signature(String uri) {
        var properties = super.getProperties();
        // 设置文件夹名称
        var auth = getQiniuAuth();
        return SignatureModel.builder()
                .accessId(properties.getKey())
                .signature(auth.uploadToken(properties.getBucket()))
                .dir(uri)
                .host(properties.getPosition())
                .policy(properties.getBucket())
                .type("qiniu")
                .build();
    }

    /**
     * 上传文件
     *
     * @param auth 认证信息
     * @param file 上传的文件信息
     */
    public void upload(Auth auth, BaseUploadModel file) {
        var properties = super.getProperties();
        var configuration = new Configuration(Region.region0());
        var uploadManager = new UploadManager(configuration);
        try {
            // 默认不指定key的情况下，以文件内容的hash值作为文件名
            uploadManager.put(file.getInputStream(), file.getFolder("/") + "/" + file.getFileName(), auth.uploadToken(properties.getBucket()), null, null);
        } catch (Exception ex) {
            throw new BusinessSimpleException("七牛云上传异常");
        }
    }

    /**
     * 获取七牛云认证信息
     *
     * @return 七牛云认证信息
     */
    private Auth getQiniuAuth() {
        var properties = super.getProperties();
        return Auth.create(properties.getKey(), properties.getSecret());
    }

}
